<template>
<div>
    <div >
       <el-button size="small" @click="Addlaber">保存</el-button>
       <el-button size="small" v-if="Yes" @click="examine">提交审批</el-button>
      </div>

    <div >
        <font size="6" bold   style="position:relative;left:700px">零星劳务单</font>
      </div>
      <br>
      
<el-form :model="apply"  label-width="100px" class="demo-ruleForm">
    <!-- 第一个表格 -->
    <table class="divtable" width="100%">
        <tr>
            <td colspan="8" class="color">基本信息</td>
        </tr>
        <tr>
            <td class="tabletd">日期:</td>
            <td><el-input type="date" v-model="apply.apply_date"  value-format="yyyy-MM-dd"  ></el-input></td>
            <td class="tabletd">零星单号编号:</td>
            <td><el-input v-model="apply.id" placeholder="保存后自动生成" disabled></el-input></td>
            <td class="tabletd">所属项目:</td>
            <td ><el-input v-model="apply.p_id" @focus="pid"   clearable></el-input></td>
            <td class="tabletd">付款方式:</td>
            <td>
                <el-select v-model="apply.type" clearable placeholder="请选择">
                    <el-option value="现金">现金</el-option>
                    <el-option value="银行转账">银行转账</el-option>
                    <el-option value="支付宝">支付宝</el-option>
                    <el-option value="支票">支票</el-option>
                    <el-option value="汇票">汇票</el-option>
                </el-select>
            </td>
        </tr>
        <tr>
            <td class="tabletd">责任人:</td>
            <td><el-input v-model="apply.handler_id"  @focus="contractsignatory"   clearable></el-input></td>
            <td class="tabletd"></td>
            <td></td>
            <td class="tabletd"></td>
            <td></td>
            <td class="tabletd"></td>
            <td></td>
        </tr>
        <tr>
            <td class="tabletd">结算说明:</td>
            <td colspan="7"><el-input v-model="apply.comment" type="textarea" rows="4"  clearable></el-input></td>
        </tr>
        
    </table>

    <multi-file-upload
                style="width: 100%;margin: 20px auto"
                title="附件"
                action="http://localhost/uploads"
                :params="{folder: 'tenderPurchasePlan'}"
                :headers="{token: token}"
                :show-button="!checkRoute"
                @uploadSuccess="handleUploadSuccess"
                :parent-file-list="fileList"
        ></multi-file-upload>

    <!-- 多行添加 -->
    <div style="width: 100%;margin: 30px auto">
            <div style="width: 100%;">
                <div style="font-weight: 700;font-size: 20px;display: flex">
                    <div>|工程量清单</div>
                    <div style="margin-left: 30px">
                        <el-button v-if="!checkRoute" size="mini" type="primary" @click="handleAddRow">增行</el-button>
                    </div>
                </div>
            </div>
            <el-divider></el-divider>
            <el-table :data="tenderPurchaseDetails">
                <el-table-column prop="index" v-if="false"></el-table-column>
                <el-table-column prop="mateId" v-if="false"></el-table-column>
                <el-table-column label="施工内容" prop="work_content">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.work_content" ></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="单位" prop="unit">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.unit" ></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="价格" prop="unit_price">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.unit_price"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="天" prop="number">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.number"  type="number"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button v-if="!checkRoute" type="danger" @click="handleRemoveRow(scope.row)">删行</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

</el-form>


    <!--所属项目子组件-->
<el-dialog  :visible.sync="dialogVisible"  width="50%">
<project-info-list  v-if="dialogVisible"  @row="project"></project-info-list>
</el-dialog>

<!-- 员工子组件 -->
<el-dialog  :visible.sync="dialogVisible2"  width="50%">
<emp-dept-post  v-if="dialogVisible2"  @row="empDeptPost"></emp-dept-post>
</el-dialog>




  </div>
</template>

<script>
import EmpDeptPost from '../common/providerinfo/EmpDeptPost.vue';
import ProjectInfoList from '../common/projectInfo/ProjectInfoList.vue';
import {DateUtil} from "@/model/common/DateUtil";

import LaoWuHeTong from '@/model/smj/LaoWuHeTong'
 const Laowuhetong= new LaoWuHeTong;

import LinXinLaoWu from '@/model/smj/LinXinLaoWu'
 const linxinlaowu= new LinXinLaoWu;


export default {
  name: "LinXinLaoWu",
  components: {ProjectInfoList,EmpDeptPost},
  directives: {},
  data() {
    return {
        apply:{
            apply_date:DateUtil.today()
        },
        tenderPurchasePlan: {
            id: '',
            date: DateUtil.today(),
            planEditor: sessionStorage.getItem('realName')
        },
        fileList:[],
        projectId:'',//所属项目id
        providerId:'',//班组id
        ctr_id:'',//合同id
        empId:'',//员工id


        dialogVisible:false,//隐藏子组件
        dialogVisible2:false,//隐藏子组件

        tenderPurchaseDetails:[],
        Yes:false,
    }
  },
  mounted() {},
  methods: {

      //转换器
      toChies(values){//形参
					let len=values.length//统计出长度
					let arr=[];
					
					let chin_list=['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']//所有的数值对应的汉字
					
					let chin_lisp=['仟','佰','拾','亿','仟','佰','拾','万','仟','佰','拾']//进制
					
					for(let i=0;i<len;i++){
						arr.push(parseInt(values[i]));		//输入的数据按下标存进去   存进去的只是数字
						arr[i]=chin_list[arr[i]]			//是根据我们输入的输入的数字，对应着我们的chin_list这个数组
					}//123['壹','佰','贰','拾','叁']
					
					for(let i=len-1,j=1;i>0;i--){//i =2	1		//倒序		为了添加进制，方便我们去观看
						arr.splice(i,0,chin_lisp[chin_lisp.length-j++])	//j=2
					}
					console.log(arr)
					
					arr=arr.join('')
					if(len>=1){
						arr+='元整'
					}
					return arr
                },
    
    //金额转换
      money(){
          var money = this.apply.agree_amount;
          var Maxmoney = this.toChies(money);
          console.log(Maxmoney)
          this.apply.agree_upper=Maxmoney
      },

      //开启隐藏项目子组件
      pid(){
           this.dialogVisible = true;
      },

      //响应项目组件
      project(row){
            this.apply.p_id = row.projectName;
            this.projectId = row.id;//获取所属项目id
            this.dialogVisible = false;
      },

      //开启隐藏员工子组件
      contractsignatory(){
          this.dialogVisible2 = true;
      },
      //响应员工子组件
      empDeptPost(row){
          this.apply.handler_id = row.name;
          this.empId = row.id;
          this.dialogVisible2 = false;
      },

      //添加一行
      handleAddRow() {
                this.tenderPurchaseDetails.push({
                    index: '',
                    work_content: '',
                    unit: '',
                    unit_price: '',
                    number:'',
                })
            },
        
        //删行
        handleRemoveRow(row) {
            this.tenderPurchaseDetails.splice(row.index, 1)
         },


         //文件上传
        handleUploadSuccess(ids,fileList){
             console.log(fileList)
                this.tenderPurchasePlan.attachemeIds = ids
            },



     

      //添加劳务合同
      Addlaber(){
          console.log(this.laber)
            //判断合同信息不能为空
          if(this.apply.apply_date == '' || this.apply.apply_date == undefined){
                this.$message.error('错误信息,日期不能为空');
          }else{
              if(this.apply.p_id == '' || this.apply.p_id == undefined){
                this.$message.error('错误信息,所属项目不能为空');
          }else{
              if(this.apply.type == '' || this.apply.type == undefined){
                this.$message.error('错误信息,付款方式不能为空');
          }else{
              if(this.apply.handler_id == '' || this.apply.handler_id == undefined){
                this.$message.error('错误信息,责任人不能为空');
          }else{
              if(this.apply.comment == '' || this.apply.comment == undefined){
                this.$message.error('错误信息,结算说明不能为空');
          }else{
              //判断工程量清单
                 let index2 =0;
                for (let index = 0; index < this.tenderPurchaseDetails.length; index++) {
                    if(this.tenderPurchaseDetails[index].work_content == ''){
                        this.$message.error('错误信息,施工内容不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].unit == ''){
                        this.$message.error('错误信息,单位不能为空');
                }else{
                    if(this.tenderPurchaseDetails[index].unit_price == ''){
                        this.$message.error('错误信息,价格不能为空');
                }else{
                     if(this.tenderPurchaseDetails[index].number == ''){
                        this.$message.error('错误信息,天数不能为空');
                }else{
                    index2++;
                }
                }
                }
                }
                }

                if(index2 == this.tenderPurchaseDetails.length ){
                    this.apply.attach_ids=this.tenderPurchasePlan.attachemeIds;
                     console.log(this.apply.attach_ids)
                          linxinlaowu.Addfragmentary(this.apply,this.projectId,this.empId).then((response)=>{
                               this.$message({ message: '添加成功',  type: 'success' });
                               this.Yes=true;
                               let id = response.data;
                               this.apply.id=id;
                               console.log(id)
                              if(this.tenderPurchaseDetails.length>0){
                                    for (let index = 0; index < this.tenderPurchaseDetails.length; index++) {
                                            linxinlaowu.AddWorkList(this.tenderPurchaseDetails[index],id).then(()=>{}) 
                                    }
                              }
                              if(this.laber.attach_ids != '' || this.laber.attach_ids != undefined){
                                  Laowuhetong.UpdateInfo(this.laber.attach_ids,sessionStorage.getItem('realName')).then(()=>{})
                              }
                          })
                }
          }
          }
          }
          }
          
      }

      },

      //提交审批
      examine(){
             Laowuhetong.FlowFragmentary(this.apply).then((response)=>{
                if(response.data == 1){
                    this.$message({ message: '提交成功',  type: 'success' });
                     this.Yes=false;
                }else{
                    this.$message.error('提交失败');
                }
                
            }) 
      },

    //   //测试
    //   Addlaber2(){
    //       this.apply.id='20210208164519'
        //   Laowuhetong.FlowFragmentary(this.apply).then((response)=>{
        //         if(response.data == 1){
        //             this.$message({ message: '提交成功',  type: 'success' });
        //              this.Yes=false;
        //         }else{
        //             this.$message.error('提交失败');
        //         }
                
        //     }) 
    //   }

  },
  computed: {
            token: {
                get() {
                    return this.$store.state.token
                }
            },
            checkRoute(){
                return Object.keys(this.$route.params).length != 0
            }
        },
  created() {
       
  },
  
};
</script>

<style>
.divtable{
    border-collapse:collapse;
}
.divtable td{
    padding:10px;
    border:1px solid rgb(7, 136, 175);
}
.tabletd{
    text-align: right;
    background-color: rgb(113, 222, 241);
}
.color{
    background-color: rgb(113, 222, 241);
}
.divtable2{
    border-collapse:collapse;
}
.divtable2 td{
    padding:10px;
    border:1px solid rgb(7, 136, 175);
}
.td{
    text-align: center;
}
</style>